package com.boyi.shared.util.http.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.protocol.HTTP;


import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

/**
 * @author 颜培轩
 * @date 2017年11月27日  上午 09:21
 */
public class HttpRequest {
    protected static Log logger = LogFactory.getLog(HttpRequest.class);

    protected String charset = HTTP.UTF_8;
    protected String contentType = "application/text";
    protected int protocolTimeoutMS = 20000;
    protected int connectTimeoutMS = 20000;
    protected Map mapHeader = null;

    public HttpRequest() {
    }

    public HttpRequest(Map mapHeader) {
        this(HTTP.UTF_8, ContentType.APPLICATION_JSON.getMimeType(), mapHeader);
    }

    public HttpRequest(String charset, String contentType) {
        this(charset, contentType, null);
    }

    public HttpRequest(String charset, String contentType, Map mapHeader) {
        this(charset, contentType, 20000, 20000, mapHeader);
    }

    public HttpRequest(String charset, String contentType, int protocolTimeoutMS, int connectTimeoutMS) {
        this(charset, contentType, protocolTimeoutMS, connectTimeoutMS, null);
    }


    public HttpRequest(String charset, String contentType, int protocolTimeoutMS, int connectTimeoutMS, Map mapHeader) {
        this.charset = charset;
        this.contentType = contentType;
        this.protocolTimeoutMS = protocolTimeoutMS;
        this.connectTimeoutMS = connectTimeoutMS;
        this.mapHeader = mapHeader;
    }

    protected StringEntity buildStringEntity(String parameters) {
        StringEntity s = null;
        if (parameters != null) {
            try {
                s = new StringEntity(parameters);
                s.setContentEncoding(charset);
                s.setContentType(contentType);
            } catch (UnsupportedEncodingException e) {
                logger.error(e.getMessage());
            }
        }
        return s;
    }

    protected void buildHeader(HttpRequestBase httpData) {
        if (mapHeader != null) {
            Iterator<Map.Entry<String, String>> iter = mapHeader.entrySet().iterator();
            Map.Entry<String, String> entry;
            while (iter.hasNext()) {
                entry = iter.next();
                httpData.setHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    protected CredentialsProvider setAuthentication(String username, String password, HttpHost httpHost) {

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        AuthScope authScope = new AuthScope(httpHost.getHostName(), httpHost.getPort());//, creds);
        Credentials creds = new UsernamePasswordCredentials(username, password);
        credsProvider.setCredentials(authScope, creds);
        return credsProvider;


    }
}
